<?php
class Wallpapermain extends Model{
	function Wallpapermain()
	{
		parent::Model();
	}
	//fetch all wall paper categeories
	function getwallpcategeories()
	{
		$this->db->order_by('update_date','desc');
		$this->db->where('parentid',0);
		$query=$this->db->get('wallpaper_categeory');
		return $query;
			
	}
	function recentcategeories()
	{
		$id=0;
		$this->db->where('parentid',$id);
		$this->db->limit('4');
		$this->db->order_by('update_date','desc');
		$query=$this->db->get('wallpaper_categeory');
		
		return $query->result_array();
	}
	function getsubcategeories($recentcategeories,$limit)
	{   $subcategeories = array();
		foreach($recentcategeories as $item)
		{
			
		 $result= $this->subcategeories($item['id'],$limit,$ascii="");
		 $subcategeories[] =  $result;
		 }
		 return $subcategeories;
	}
	function getimages($subcategeories)
	{
		$image = array();
       foreach($subcategeories as $item)
		{ 
		  foreach($item as $item1)
	     { 			
		 $result1= $this->getimages1($item1['id'],$limit=0);
         $image[$item1['id']]=$result1[0]['id'];
		 }
	    }
	    return $image; 
    }
	function subcategeories($id,$limit,$ascii)
	{	
		
		$this->db->where('parentid',$id);
		if($ascii!="")
		$this->db->like('upper(catname)',chr($ascii),'after');
		
		if($limit!=0)
		{
		$this->db->limit($limit);
		}
		$this->db->order_by('catname','asc');
		$this->db->order_by('update_date','desc');
		$query=$this->db->get('wallpaper_categeory');
		return $query->result_array();
		
		
	}
	//getting recent image id per subcategeory
	function getimages1($id)
	{
		$this->db->where('parentid',$id);
		$this->db->select_max('id');	
		$query=$this->db->get('wallpaper_images');
		return $query->result_array();
	}
	function getallimages($id)
	{
		//$this->db->order_by('update_date','desc');
		$this->db->order_by('id','desc');
		$this->db->where('parentid',$id);
		$query=$this->db->get('wallpaper_images');
		return $query->result_array();
	}
	function getcategeoryname($id)
	{
		$this->db->where('id',$id);
		$this->db->select('catname');
		$query=$this->db->get('wallpaper_categeory');
		return $query->result_array();
	}
	function get_subcategeories($id)
	{
		if($id==0)
		{
			$this->db->where('id > 4');
			$this->db->where('parentid > 4');
		}
		else 
			$this->db->where('wallpaper_categeory.parentid',$id);
		$this->db->limit(5);
		$this->db->order_by('update_date','desc');
		//$this->db->select('wallpaper_categeory.id,parentid');
		$query=$this->db->get('wallpaper_categeory');
		$sub_categeories=$query->result();
		return $sub_categeories;
		
	}
	function homepage_image($id)
	{
//		$this->db->where('wallpaper_categeory.parentid',$id);
//		$this->db->limit(5);
//		$this->db->order_by('id','desc');
//		$this->db->select('wallpaper_images.id');
//		$this->db->from('wallpaper_images');
//		$this->db->join('wallpapr_csategeory','wallpaper_categeory.id=wallpaper_images.parentid');
//		$query=$this->db->get();
       
        $this->db->order_by('id','desc');
        $this->db->limit(1);
        $this->db->where('parentid',$id);
        $query = $this->db->get('wallpaper_images');
		$wallpaperimage=$query->result();
		return $wallpaperimage;
	}
}
?>